﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using ThouShaltNotBrick.Logic.GeneralObjects;
using ThouShaltNotBrick.Logic.Helpers;
using GameUtils.Input;
using GameControls;
using Microsoft.Xna.Framework.Graphics;

namespace ThouShaltNotBrick.Logic.GameStates
{
    internal sealed class PlayNormalGameState : PlayGameStateBase
    {
        #region Constants
        #endregion

        #region Properties
        private Label mScoreLabel;
        private Label mLivesLabel;
        #endregion

        #region Events
        #endregion

        #region Constructors
        #endregion

        #region Overriden Methods
        protected override void InitializeImpl()
        {
            base.InitializeImpl();

            mIsMouseVisible = false;

            InitializeControls();
        }

        protected override void DestroyImpl()
        {
            DestroyControls();

            base.DestroyImpl();
        }

        protected override void StartImpl(GameStateChangeData pChangeData)
        {
            SetPlayData(pChangeData);
        }

        protected override void StopImpl()
        {
        }

        protected override void ResumeImpl(GameStateChangeData pChangeData)
        {
            SetPlayData(pChangeData);
        }

        protected override void PauseImpl()
        {
        }

        protected override void HandleInputImpl(GameTime pGameTime)
        {
            Pad pad = mPlayData.CurrentLevel.Pad;
            pad.Position = Constants.LEVEL_WIDTH * PollingInput.X / mGameManager.GetClientWidth();
        }

        protected override void UpdateImpl(GameTime pGameTime)
        {
            UpdateReturnValue updateReturnValue = mLevelUpdater.Update(pGameTime, mPlayData);
            if (updateReturnValue.IsLifeLost)
            {
                ChangeToAnotherPlayState(GameStateType.PlayLoss);
            }
            else if (mPlayData.CurrentLevel.IsLevelClear())
            {
                ChangeToAnotherPlayState(GameStateType.PlayWin);
            }

            mScoreLabel.Text = mPlayData.Score.ToString("000000");
            mLivesLabel.Text = "Lives: " + mPlayData.Lives.ToString();
        }

        protected override void DrawImpl(GameTime pGameTime)
        {
            mLevelDrawer.Draw(pGameTime, mPlayData);
        }
        #endregion

        #region Methods
        private void InitializeControls()
        {
            DestroyControls();

            int screenWidth = mGameManager.GetClientWidth();
            int screenHeight = mGameManager.GetClientHeight();

            SpriteFont scoreFont = mFontResources.Font20;
            Vector2 scoreLabelSize = scoreFont.MeasureString("00000000");
            int scoreLabelWidth = (int)scoreLabelSize.X;
            int scoreLabelHeight = (int)scoreLabelSize.Y;
            Rectangle scoreLabelRect = new Rectangle(0, 0, scoreLabelWidth, scoreLabelHeight);
            mScoreLabel = new Label(scoreLabelRect);
            mScoreLabel.SetText(string.Empty, mFontResources.Font20, Color.Red);
            mScoreLabel.HorizontalAlignment = HorizontalAlignment.Center;
            mScoreLabel.VerticalAlignment = VerticalAlignment.Center;
            mControls.Add(mScoreLabel);

            SpriteFont livesLabel = mFontResources.Font20;
            Vector2 livesLabelSize = livesLabel.MeasureString("++Lives: 00++");
            int livesLabelWidth = (int)livesLabelSize.X;
            int livesLabelHeight = (int)livesLabelSize.Y;
            Rectangle livesLabelRect = new Rectangle(screenWidth - livesLabelWidth, 0, livesLabelWidth, livesLabelHeight);
            mLivesLabel = new Label(livesLabelRect);
            mLivesLabel.SetText(string.Empty, mFontResources.Font20, Color.Red);
            mLivesLabel.HorizontalAlignment = HorizontalAlignment.Center;
            mLivesLabel.VerticalAlignment = VerticalAlignment.Center;
            mControls.Add(mLivesLabel);
        }

        private void DestroyControls()
        {
            mControls.Clear();
        }
        #endregion

        #region Event Handlers
        #endregion

        #region Inner Classes
        #endregion
    }
}